package com.ruoyi.project.remake.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.project.common.utils.DictUtils;
import com.ruoyi.project.remake.domain.TCaseMain;
import com.ruoyi.project.remake.domain.constant.InitConstant;
import com.ruoyi.project.remake.domain.dto.CopyCaseDto;
import com.ruoyi.project.remake.domain.query.SelfMachine;
import com.ruoyi.project.remake.domain.utils.ApplyUtils;
import com.ruoyi.project.remake.domain.vo.CaseMainVo;
import com.ruoyi.project.remake.domain.vo.CopyCaseVo;
import com.ruoyi.project.remake.service.ITCaseMainService;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.remake.mapper.TApplyStatusMapper;
import com.ruoyi.project.remake.domain.TApplyStatus;
import com.ruoyi.project.remake.service.ITApplyStatusService;

/**
 * 病例申请打印Service业务层处理
 *
 * @author 孙山伟
 * @date 2020-12-26
 */
@Service
public class TApplyStatusServiceImpl implements ITApplyStatusService {
    @Autowired
    private TApplyStatusMapper tApplyStatusMapper;
    @Autowired
    private ITCaseMainService caseMainService;
    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 查询病例申请打印
     *
     * @param id 病例申请打印ID
     * @return 病例申请打印
     */
    @Override
    public TApplyStatus selectTApplyStatusById(Long id) {
        return tApplyStatusMapper.selectTApplyStatusById(id);
    }

    /**
     * 查询病例申请打印列表
     *
     * @param tApplyStatus 病例申请打印
     * @return 病例申请打印
     */
    @Override
    public List<TApplyStatus> selectTApplyStatusList(TApplyStatus tApplyStatus) {
        return tApplyStatusMapper.selectTApplyStatusList(tApplyStatus);
    }

    /**
     * 新增病例申请打印
     *
     * @param tApplyStatus 病例申请打印
     * @return 结果
     */
    @Override
    public int insertTApplyStatus(TApplyStatus tApplyStatus) {
        try {
            Long[] ids = tApplyStatus.getCaseMainIds();
            for (Long caseMainId : ids) {
                TApplyStatus entity = new TApplyStatus();
                BeanUtil.copyProperties(tApplyStatus, entity);
                entity.setCaseMainId(caseMainId);
                CaseMainVo caseMain=caseMainService.selectTCaseMainById(caseMainId);
                entity.setPrintStatus(InitConstant.APPLY_STATUS);
                entity.setApplyNumber(ApplyUtils.genApplyNum(InitConstant.BACK_APPLY));
                entity.setCardId(caseMain.getCardId());
                entity.setMedicalCardId(caseMain.getMedicalCardId());
                tApplyStatusMapper.insertTApplyStatus(entity);
            }
        } catch (Exception e) {

            return 0;
        }
        return 1;
    }

    /**
     * 修改病例申请打印
     *
     * @param tApplyStatus 病例申请打印
     * @return 结果
     */
    @Override
    public int updateTApplyStatus(TApplyStatus tApplyStatus) {
        return tApplyStatusMapper.updateTApplyStatus(tApplyStatus);
    }

    /**
     * 批量删除病例申请打印
     *
     * @param ids 需要删除的病例申请打印ID
     * @return 结果
     */
    @Override
    public int deleteTApplyStatusByIds(Long[] ids) {
        return tApplyStatusMapper.deleteTApplyStatusByIds(ids);
    }

    /**
     * 删除病例申请打印信息
     *
     * @param id 病例申请打印ID
     * @return 结果
     */
    @Override
    public int deleteTApplyStatusById(Long id) {
        return tApplyStatusMapper.deleteTApplyStatusById(id);
    }

    @Override
    public List<CopyCaseVo> selectCopyPrintList(CopyCaseDto copyCaseDto) {

        List<CopyCaseVo> copyCaseVoList=tApplyStatusMapper.selectCopyPrintList(copyCaseDto);
        List<SysDictData> dictDataList=dictDataService.selectDictDataByType(InitConstant.APPROLVAL_STATUS);
        for(CopyCaseVo vo:copyCaseVoList){
            vo.setStatus(DictUtils.getDiactLabel(dictDataList,vo.getStatus()));
        }
        return copyCaseVoList;
    }

    @Override
    public void seflApply(SelfMachine selfMachine) {
        TApplyStatus applyStatus=new TApplyStatus();
        applyStatus.setCardId(selfMachine.getCardId());
        applyStatus.setMedicalCardId(selfMachine.getMedicalCardId());
        applyStatus.setPrintStatus(InitConstant.APPLY_STATUS);
        applyStatus.setApplyNumber(ApplyUtils.genApplyNum(InitConstant.BACK_APPLY));
        tApplyStatusMapper.insertTApplyStatus(applyStatus);
    }
}
